#coding: utf8
import re
import locale
from pygeocoder import Geocoder
import time
import urlparse
from datetime import datetime
from scrapy.http import Request
from scrapy.http import FormRequest
from scrapy.spider import BaseSpider
from scrapy.selector import HtmlXPathSelector
from fresh.items import RestaurantItem
from fresh.items import RestaurantReview

class TripUtils(object):

    utils = None
    dictMeses = {"enero": "/01/","febrero":"/02/","marzo":"/03/","abril":"/04/","mayo":"/05/","junio":"/06/","julio":"/07/","agosto":"/08/","septiembre":"/09/","octubre":"/10/","noviembre":"/11/","diciembre":"/12/"}

    def fillRestaurant(self,response,hxs,restaurant):
        try:
            if response.url.find('Barcelona_Catalonia') != 1:
                city="BCN"
            elif response.url.find('Madrid') != 1:
                city="MAD"
            else:
                city="NA"
            restaurant['reviews_total']= self.__getReviewsTotal(hxs.select('//div[@id="listing_main"]/div[@class="full_wrap"]/div[@class="col2of2"]/div[@class="rs rating"]/a/span/text()').extract())
            restaurant['reviews'] = []
            restaurant['city']=city                
            restaurant['restaurant_name'] = hxs.select('//div[@id="HEADING_GROUP"]/div[@id="headingWrapper"]/h1/text()').extract()
            address0 = hxs.select('//span[@class="format_address"]/span[@class="street-address"]/text()').extract()
            address1 = hxs.select('//span[@class="format_address"]/span[@class="locality"]/span[@property="v:locality"]/text()').extract()
            address2 = hxs.select('//span[@class="format_address"]/span[@class="locality"]/span[@property="v:postal-code"]/text()').extract()
            address = self.utils.convertListToString(address0) + ", " + self.utils.convertListToString(address1) + ", " + self.utils.convertListToString(address2)
            restaurant['address'] = address
            restaurant['address_google'] = self.utils.getRestAddressGoogleReady(address)
            restaurant['phone'] = self.utils.getPhone(hxs.select('//div[@class="sprite-greenPhone fl icnLink"]/../div[@class="fl"]/text()').extract())
            restaurant['cp']= hxs.select('//span[@class="format_address"]/span[@class="locality"]/span[@property="v:postal-code"]/text()').extract()
            restaurant['score']=self.__getScore(hxs.select('//div[@id="listing_main"]/div[@class="full_wrap"]/div[@class="col2of2"]/div[@class="rs rating"]/span/img/@content').extract())
            restaurant['score_comida']=self.__getSubScore(hxs.select('//fieldset[@class="review_filter_eatery"]/div[@class="content wrap"]/div[@class="col2of2 composite"]/ul[@class="barChart"]/div[@class="wrap row"][1]/div[@class="line"]/div[@class="fill"]/@style').extract())
            restaurant['score_servicio']=self.__getSubScore(hxs.select('//fieldset[@class="review_filter_eatery"]/div[@class="content wrap"]/div[@class="col2of2 composite"]/ul[@class="barChart"]/div[@class="wrap row"][2]/div[@class="line"]/div[@class="fill"]/@style').extract())
            restaurant['score_calidadP']=self.__getSubScore(hxs.select('//fieldset[@class="review_filter_eatery"]/div[@class="content wrap"]/div[@class="col2of2 composite"]/ul[@class="barChart"]/div[@class="wrap row"][3]/div[@class="line"]/div[@class="fill"]/@style').extract())
            restaurant['score_atmosfera']=self.__getSubScore(hxs.select('//fieldset[@class="review_filter_eatery"]/div[@class="content wrap"]/div[@class="col2of2 composite"]/ul[@class="barChart"]/div[@class="wrap row"][4]/div[@class="line"]/div[@class="fill"]/@style').extract())
            restaurant['url_rest']=None
            #restaurant['email']=self.__getEmail(

            """list of types of restaurant"""
            restaurant['rest_type']=self.__getRestType(hxs.select('//b/text()[contains(.,"Cocina:")]/../..'))

            return


        except Exception,e:
            self.utils.logProcessDetail("Error in fillRestaurant from response: " + str(e),self.utils.ERROR)
          
    

    def fillRestaurantCommentTranslationGoogle(self,response,hxs,restaurant,restaurantReview):
        try:
                                              
            restaurantReview['title'] = hxs.select('//b[@class="dt3"]/text()').extract()
            restaurantReview['comment'] = hxs.select('(//td[@class="preview_box"]/div)[3]/div/text()').extract()

            if len(self.utils.convertListToString(restaurantReview['title']))!=0 or len(self.utils.convertListToString(restaurantReview['comment']))!=0:
                restaurant['reviews'].append(restaurantReview)
            return restaurantReview


        except Exception,e:
            self.utils.logProcessDetail("Error in fillRestaurantCommentTranslationGoogle en TRIP-UTILS from response: " + str(e),self.utils.ERROR)

    def __getReviewsTotal(self,total):
        sTotal=self.utils.convertListToString(total)
        if sTotal is not None and len(sTotal)>0:
            sTotal=sTotal.replace(".","")
        return sTotal
    
    
    def __getTitleTranslationCustom(self,title):
        try:
            if len(title)>2:
                tmp=self.utils.cleanString(title[1])
                return tmp.replace(":","").strip()
            
                
        except Exception,e:
            self.utils.logProcessDetail("Error in __getTitleTranslationCustom en TRIP-UTILS: " + str(e),self.utils.ERROR)
    
            

    def fillRestaurantCommentTranslationCustom(self,response,hxs,restaurant,restaurantReview):
        try:



            
                                              
            restaurantReview['title'] = self.__getTitleTranslationCustom(hxs.select('(//table/tr/td[@class="dt1"]/div)[2]/b/text()').extract())
            restaurantReview['comment'] =hxs.select('(//table/tr/td[@class="dt3"]/div)[2]/div/text()').extract()

            
            #self.utils.logProcessDetail("-----------------------comment: "+str(restaurantReview) , self.utils.INFO)    
            #/html/body/div[3]/table/tbody/tr/td/table/tbody/tr[2]/td/div[2]/div
            #hxs.select('(//tbody/tr/td[@class="dt3"]/div)[2]/div/text()').extract()

            #theoretically it should be all in, in case there are blanks QA will detect and inform
            restaurant['reviews'].append(restaurantReview)
            return restaurantReview


        except Exception,e:
            self.utils.logProcessDetail("Error in fillRestaurantCommentTranslation en TRIP-UTILS from response: " + str(e),self.utils.ERROR)


    def fillRestaurantComment(self,response,hxs,restaurant):
        try:
            
            #from scrapy.shell import inspect_response
            #inspect_response(response)
            

            
            restaurantReview = RestaurantReview()
            restaurantReview['author'] = hxs.select('(div[@class="col1of2"]/div[@class="member_info"]/div)[1]/div[@class="username mo"]/span/span/text()').extract()
            """ maybe an anonymous user"""
            if len(restaurantReview['author'])==0:
                restaurantReview['author'] = hxs.select('(div[@class="col1of2"]/div[@class="member_info"]/div)[1]/div[@class="username mo"]/span/text()').extract()
            if len(restaurantReview['author'])==0:                
                restaurantReview['author'] = hxs.select('div[@class="col1of2"]/div[@class="member_info"]/div[@class="username"]/text()').extract()
                
            restaurantReview['date'] = self.__getCommentDate(self.utils.convertListToString(hxs.select('div[@class="col2of2"]/div[@class="innerBubble"]/div[@class="rating reviewItemInline"]/span[@class="ratingDate"]/text()').extract()))
            
            restaurantReview['score'] = self.__getScoreComment(self.utils.convertListToString(hxs.select('(div[@class="col2of2"]/div[@class="innerBubble"]/div[@class="rating reviewItemInline"]/span)[1]/img/@class').extract()))
            
            restaurantReview['title'] = hxs.select('div[@class="col2of2"]/div[@class="innerBubble"]/div[@class="quote"]/text()').extract()
            restaurantReview['comment'] = hxs.select('div[@class="col2of2"]/div[@class="innerBubble"]/div[@class="entry"]/p/text()').extract()
            
            restaurantReview['score_calidadP']=self.__getSubScoreComment(self.utils.convertListToString(hxs.select('(div[@class="col2of2"]/div[@class="innerBubble"]/div[@class="rating-list"]/ul/li/ul[@class="recommend-column first"]/li)[1]/span/img/@class').extract()))
            restaurantReview['score_ambiente']=self.__getSubScoreComment(self.utils.convertListToString(hxs.select('(div[@class="col2of2"]/div[@class="innerBubble"]/div[@class="rating-list"]/ul/li/ul[@class="recommend-column first"]/li)[2]/span/img/@class').extract()))
            restaurantReview['score_servicio']=self.__getSubScoreComment(self.utils.convertListToString(hxs.select('(div[@class="col2of2"]/div[@class="innerBubble"]/div[@class="rating-list"]/ul/li/ul[@class="recommend-column"]/li)[1]/span/img/@class').extract()))
            restaurantReview['score_comida']=self.__getSubScoreComment(self.utils.convertListToString(hxs.select('(div[@class="col2of2"]/div[@class="innerBubble"]/div[@class="rating-list"]/ul/li/ul[@class="recommend-column"]/li)[2]/span/img/@class').extract()))

            restaurant['cp']=None
            restaurant['url_rest']=None

            #just add review in case title or comment is correctly filled-in otherwise return partially filledin
            if len(self.utils.convertListToString(restaurantReview['title']))!=0 or len(self.utils.convertListToString(restaurantReview['comment']))!=0:
                restaurant['reviews'].append(restaurantReview)
                
            return restaurantReview


        except Exception,e:
            self.utils.logProcessDetail("Error in fillRestaurantComment en TRIP-UTILS from response: " + str(e),self.utils.ERROR)


    def __getCommentDate(self,inDate):
        try:
            dDate=None
            tmp_s=""
            if inDate is not None and len(inDate)>0:
                po = inDate.find("escrita el ")
                if po != -1:
                    tmp_s=inDate[po+11:]
                    tmp_s=tmp_s.strip().lower()
                    for k, v in self.dictMeses.items():
                        tmp_s = tmp_s.replace(k, v)
                    tmp_s=tmp_s.replace(" ","")
                    dDate = datetime.strptime(tmp_s, '%d/%m/%Y').date()
                    tmp_s = dDate.strftime('%d/%m/%Y')
                    """
                    self.utils.log("DEBUG","po: " + str(po) + "Date: " + str(dDate),self.utils.DEBUG)
                    txtDate=dDate.strftime('%d/%m/%Y')
                    """      
            return tmp_s
    
        except Exception,e:
            self.utils.logProcessDetail("Error in __getCommentDate: " + str(e) + "date: " + str(inDate),self.utils.ERROR)


    def __getRestType(self,hxs):
        try:
            d=dict()
            s = self.utils.convertListToString(hxs.select('text()').extract())
            ini=0
            end=0
            while end != -1:
                end = s.find(",")
                if end != -1:
                    d[s[ini:end].strip()]=0
                    s = s[end+1:]
                else:
                    d[(s.strip())]=0

            return d.keys()

        except Exception,e:
            self.utils.logProcessDetail("Error in __getRestType: " + str(e),self.utils.ERROR)

    def __getScore(self,txtScore):
        tmp_s=""
        if len(txtScore)>0:
            tmp_s = str(txtScore[0])
            """tmp_s = str(Decimal(tmp_s)*2)"""
        return tmp_s
            
    def __getSubScore(self,txtScore):
        tmp_s=""
        if len(txtScore)>0:
            tmp_s = str(txtScore[0])
            tmp_s = tmp_s.replace("width:","")
            tmp_s = tmp_s.replace("px;","")
            """tmp_s = str(Decimal(tmp_s)/10)"""
            
        return tmp_s

    def __getSubScoreComment(self,txtScore):
        tmp_s=""
        if len(txtScore)>0:
            tmp_s = str(txtScore[0])

            po = txtScore.find("ss10")
            if po != -1:
                tmp_s = "1"
                return tmp_s
            po = txtScore.find("ss20")
            if po != -1:
                tmp_s = "2"
                return tmp_s
            po = txtScore.find("ss30")
            if po != -1:
                tmp_s = "3"
                return tmp_s
            po = txtScore.find("ss40")
            if po != -1:
                tmp_s = "4"
                return tmp_s
            po = txtScore.find("ss50")
            if po != -1:
                tmp_s = "5"
                return tmp_s

            po = txtScore.find("s10")
            if po != -1:
                tmp_s = "1"
                return tmp_s
            po = txtScore.find("s20")
            if po != -1:
                tmp_s = "2"
                return tmp_s
            po = txtScore.find("s30")
            if po != -1:
                tmp_s = "3"
                return tmp_s
            po = txtScore.find("s40")
            if po != -1:
                tmp_s = "4"
                return tmp_s
            po = txtScore.find("s50")
            if po != -1:
                tmp_s = "5"
                return tmp_s
        return tmp_s

    def __getScoreComment(self,txtScore):
        return self.__getSubScoreComment(txtScore)
    

           


        